<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Models\Role;
use Excel;

class RoleController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $keyword = $request->get('keyword','');
        $export = $request->get('export',0);

        $builder = Role::where([]);
        if(!empty($keyword)){
            $builder->where('name','like','%'.$keyword.'%');
        }
        $builder->orderBy('id','desc');

        if($export == 1){
            $list = $builder->get();
            //清空缓冲区
            ob_clean();
            Excel::create(date('YmdHis').'_导出结果', function($excel) use($list) {
                 $excel->sheet('Sheet1', function($sheet) use($list) {
                     $headers = ['角色'];
                     $sheet->row(1,$headers);
                     $row = 2;
                     foreach ($list as $model) {
                         $sheet->row($row++,[
                            $model->name
                        ]);
                     }
                 });
            })->download('xls');
        }else{
            $pager = $this->build_pager_data($builder->paginate(10));
            return response()->json(json_success($pager));
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = array_null_to_empty($request->only(['name','permissions']));
        $data['permissions'] = json_encode($data['permissions']);

        $role = new Role();
        $role->fill($data);
        $role->save();
        return response()->json(json_success(['id'=>$role->id]));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Role $role)
    {
        return response()->json(json_success($role));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Role $model)
    {
        $data = array_null_to_empty($request->only(['name','permissions']));
        $model->update($data);
        return response()->json(json_success());
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Role $model)
    {
        $model->delete();
        return response()->json(json_success());
    }
}
